package com.controller;

import com.mapper.StudentMapper;
import com.mapper.TeacherMapper;
import com.mapper.TopicMapper;
import com.mapper.UserMapper;
import com.pojo.SClass;
import com.pojo.Student;
import com.pojo.Teacher;
import com.pojo.Topic;
import com.utils.Tool;
import com.utils.WxTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentMapper sm;
    @Autowired
    TopicMapper tm;
    @Autowired
    UserMapper um;

    /**
     * 分页获取学生列表
     *
     */
    @RequestMapping("/getList")
    public Map<String,Object> getList() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

            List<Student> list = sm.getList();
            for (Student item : list) {
                SClass sClass = sm.getClass(item.getClass_id());
                item.setPassword(null);
                item.setClass_name(sClass.getName());
                item.setSdept_name(sm.getSdeptName(sClass.getSdept_id()));
            }
            map.put("total",sm.countStudents());
            map.put("data",list);

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    // 获取系部列表
    @RequestMapping("/getSdepts")
    public Map<String,Object> getSdepts() {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        map.put("code",code);
        map.put("msg",msg);
        map.put("data",sm.getSdepts());
        return map;
    }

    @RequestMapping("/getStuTopic")
    public Map<String,Object> getStuTopic(String open_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";
        if(open_id==null){
            code=501;
            msg="open_id不能为空！";
        }else{
            List<Topic> list=tm.getByStu_id(open_id);
            for (Topic topic : list) {
                Teacher teacher = um.getTeacherByOpenId(topic.getTeacher_id());
                teacher.setPassword(null);
                teacher.setLoginTime(null);
                topic.setTeacher(teacher);
            }
            map.put("data",list);
        }
        map.put("code",code);
        map.put("msg",msg);

        return map;
    }

    /**
     * 添加系部
     * @param name 系部名
     */
    @RequestMapping("/addSdept")
    public Map<String,Object> addSdept(String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (name == null) {
            code = 500;
            msg = "name 不能为空";
        } else if (sm.getSdeptByName(name) != null) {
            code = 501;
            msg = "系部已存在";
        } else {
            sm.addSdept(name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改系部
     * @param id 系部 id
     * @param name 系部名
     */
    @RequestMapping("/editSdept")
    public Map<String, Object> editSdept(Integer id,String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (name == null) {
            code = 501;
            msg = "name 不能为空";
        } else if (sm.getSdeptById(id) == null) {
            code = 502;
            msg = "系部不存在";
        } else if (sm.getSdeptByName(name) != null) {
            code = 503;
            msg = "系部名已存在";
        } else {
            sm.editSdept(id,name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除系部
     * @param id 系部 id
     */
    @RequestMapping("/delSdept")
    public Map<String,Object> delSdept(Integer id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (id == null) {
            code = 500;
            msg = "id 不能为空";
        } else if (sm.getSdeptById(id) == null) {
            code = 501;
            msg = "系部不存在";
        } else {
            sm.delSdept(id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 获取班级列表
     * @param sdept_id 系部 id
     */
    @RequestMapping("/getClassBySdept")
    public Map<String,Object> getClassBySdept(Integer sdept_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (sdept_id == null) {
            code = 500;
            msg = "sdept_id 不存在";
        } else if (sm.getSdeptById(sdept_id) == null) {
            code = 501;
            msg = "系部不存在";
        } else {
            map.put("data",sm.getClassBySdept(sdept_id));
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 添加班级
     * @param sdept_id 所属系部 id
     * @param name 班级名
     */
    @RequestMapping("/addClass")
    public Map<String,Object> addClass(Integer sdept_id,String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (sdept_id == null) {
            code = 500;
            msg = "sdept_id 不能为空";
        } else if (name == null) {
            code = 501;
            msg = "name 不能为空";
        } else if (sm.getSdeptById(sdept_id) == null) {
            code = 502;
            msg = "系部不存在";
        } else if (sm.getClassBySdeptIdAndClassName(sdept_id, name) != null) {
            code = 503;
            msg = "班级名已在本系部中存在";
        } else {
            sm.addClass(sdept_id, name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 修改班级
     * @param class_id 班级 id
     * @param name 班级名
     */
    @RequestMapping("/editClass")
    public Map<String,Object> editClass(Integer class_id,String name) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (class_id == null) {
            code = 500;
            msg = "class_id 不能为空";
        } else if (name == null) {
            code = 501;
            msg = "name 不能为空";
        } else if (sm.getClassById(class_id) == null) {
            code = 502;
            msg = "班级不存在";
        } else {
            sm.editClass(class_id,name);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 删除班级
     * @param class_id 班级 id
     */
    @RequestMapping("/delClass")
    public Map<String,Object> delClass(Integer class_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (class_id == null) {
            code = 500;
            msg = "class_id 不能为空";
        } else if (sm.getClassById(class_id) == null) {
            code = 501;
            msg = "班级不存在";
        } else {
            sm.delClass(class_id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

    /**
     * 注册
     * @param open_id open_id
     * @param gender 性别 0-女 1-男
     * @param nickname 昵称
     * @param avatar 头像
     * @param account 手机号
     * @param stunum 学号
     * @param class_id 班级 id
     */
    @RequestMapping("/register")
    public Map<String,Object> register(String open_id,Integer gender,String nickname,String avatar,String account,String stunum,Integer class_id) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(open_id)) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (sm.getInfo(open_id) != null) {
            code = 501;
            msg = "用户已存在";
        } else if (gender == null) {
            code = 502;
            msg = "性别不能为空";
        } else if (Tool.isNull(nickname)) {
            code = 503;
            msg = "昵称不能为空";
        } else if (Tool.isNull(avatar)) {
            code = 504;
            msg = "头像不能为空";
        } else if (Tool.isNull(account)) {
            code = 505;
            msg = "手机号不能为空";
        } else if (sm.getByAccount(account) != null) {
            code = 506;
            msg = "手机号已存在";
        } else if (Tool.isNull(stunum)) {
            code = 507;
            msg = "学号不能为空";
        } else if (sm.getByStunum(stunum) != null) {
            code = 508;
            msg = "学号已存在";
        } else if (class_id == null) {
            code = 509;
            msg = "班级不能为空";
        } else {
            String password = Tool.generateMd5(stunum + account);
            sm.register(open_id,gender,nickname,avatar,account,password,stunum,class_id);
        }

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }


    /**
     * 修改姓名
     * @param open_id 学生 id
     * @param nickname 姓名
     */
    @RequestMapping("/editNickname")
    public Map<String,Object> editNickname(String open_id,String nickname) {
        Map<String,Object> map = new HashMap<>();
        int code = 200;
        String msg = "ok";

        if (Tool.isNull(open_id)) {
            code = 500;
            msg = "open_id 不能为空";
        } else if (sm.getInfo(open_id) == null) {
            code = 501;
            msg = "学生不存在";
        } else if (Tool.isNull(nickname)) {
            code = 502;
            msg = "姓名不能为空";
        } else sm.editNickname(open_id, nickname);

        map.put("code",code);
        map.put("msg",msg);
        return map;
    }

}
